
#include "kodes.h"
#include "xsystem.h"
#include <boost/assert.hpp>


ElXSys::ElXSys(Signaro baseSig)
: pending_(0)
{
  baseSignaro_ = LegoKonverto::getBySignaro(baseSig);
  BOOST_ASSERT(baseSignaro_);
}

ElXSys::~ElXSys()
{
  delete baseSignaro_;
}

void ElXSys::setStream(istream *s)
{
  BOOST_ASSERT(pending_ == 0);
  pending_ = 0;
  BOOST_ASSERT(baseSignaro_);
  baseSignaro_->setStream(s);
}


wchar_t ElXSys::doni()
{
  wchar_t c = baseSignaro_->doni();

  if (!c)
  {
    if (pending_)
    {
      c = pending_;
      pending_ = 0;
    }
    return static_cast<wchar_t>(c);
  }

  if (!pending_)
  {
    pending_ = c;
    c = baseSignaro_->doni();
  }

  if (pending_ && (c == 'x' || c == 'X'))
  {
    switch (pending_)
    {
    case 'c':
      pending_ = 0; 
      return 0x0109;
    case 'C':
      pending_ = 0; 
      return 0x0108;
    case 'g': 
      pending_ = 0; 
      return 0x011D;
    case 'G': 
      pending_ = 0; 
      return 0x011C;
    case 'h': 
      pending_ = 0; 
      return 0x0125;
    case 'H': 
      pending_ = 0; 
      return 0x0124;
    case 'j': 
      pending_ = 0; 
      return 0x0135;
    case 'J': 
      pending_ = 0; 
      return 0x0134;
    case 's': 
      pending_ = 0; 
      return 0x015D;
    case 'S': 
      pending_ = 0; 
      return 0x015C;
    case 'u': 
      pending_ = 0; 
      return 0x016D;
    case 'U': 
      pending_ = 0; 
      return 0x016C;
    }
  }

  wchar_t res = pending_;
  pending_ = c;
  return res;
}












AlXSys::AlXSys(Signaro baseSig)
{
  baseSignaro_ = ScriboKonverto::getBySignaro(baseSig);
  BOOST_ASSERT(baseSignaro_);
}

AlXSys::~AlXSys()
{
  delete baseSignaro_;
}

void AlXSys::setStream(ostream *s)
{
  BOOST_ASSERT(baseSignaro_);
  baseSignaro_->setStream(s);
}

void AlXSys::meti(wchar_t c)
{
  switch (c)
  {
    case 0x0109:
      baseSignaro_->meti('c');
      baseSignaro_->meti('x');
      break;
    case 0x0108:
      baseSignaro_->meti('C');
      baseSignaro_->meti('X');
      break;
    case 0x011D:
      baseSignaro_->meti('g');
      baseSignaro_->meti('x');
      break;
    case 0x011C:
      baseSignaro_->meti('G');
      baseSignaro_->meti('X');
      break;
    case 0x0125:
      baseSignaro_->meti('h');
      baseSignaro_->meti('x');
      break;
    case 0x0124:
      baseSignaro_->meti('H');
      baseSignaro_->meti('X');
      break;
    case 0x0135:
      baseSignaro_->meti('j');
      baseSignaro_->meti('x');
      break;
    case 0x0134:
      baseSignaro_->meti('J');
      baseSignaro_->meti('X');
      break;
    case 0x015D:
      baseSignaro_->meti('s');
      baseSignaro_->meti('x');
      break;
    case 0x015C:
      baseSignaro_->meti('S');
      baseSignaro_->meti('X');
      break;
    case 0x016D:
      baseSignaro_->meti('u');
      baseSignaro_->meti('x');
      break;
    case 0x016C:
      baseSignaro_->meti('U');
      baseSignaro_->meti('X');
      break;
    default:
      baseSignaro_->meti(c);
  }
}

